<template>
  <el-dialog
      :title="state.dialog.title"
      v-model="state.dialog.isShowDialog"
      width="750px"
      custom-class="dialog-custom"
      @close="closeDialog"
  >
    <div class="dialog-content">
      <el-form
          ref="dataFormRef"
          :model="state.dataForm"
          :rules="state.rules"
          label-width="100px"
          label-position="top"
          size="default"
      >
        <el-form-item label="姓名" prop="name">
          <el-input
              v-model="state.dataForm.name"
              placeholder="请输入姓名"
              clearable
              class="input-full-width"
          ></el-input>
        </el-form-item>
        <el-form-item label="卡号" prop="cardNo">
          <el-input
              v-model="state.dataForm.cardNo"
              placeholder="请输入卡号"
              clearable
              class="input-full-width"
          ></el-input>
        </el-form-item>
        <el-form-item label="工作单位" prop="workUnit">
          <el-input
              v-model="state.dataForm.workUnit"
              placeholder="请输入工作单位"
              clearable
              class="input-full-width"
          ></el-input>
        </el-form-item>


        <el-form-item label="工作地点" prop="workPlace">
          <el-input
              v-model="state.dataForm.workPlace"
              placeholder="请输入工作地点"
              clearable
              class="input-full-width"
          ></el-input>
        </el-form-item>
      </el-form>
    </div>

    <div class="dialog-action">
      <el-button type="text" @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </div>
  </el-dialog>
<!--	<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="750px" @close="closeDialog">-->
<!--		<el-form ref="dataFormRef" :model="state.dataForm" :rules="state.rules" size="default" label-width="90px">-->
<!--			<el-row :gutter="35">-->
<!--				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">-->
<!--					<el-form-item label="姓名" prop="name">-->
<!--						<el-input v-model="state.dataForm.name" placeholder="请输入姓名" clearable></el-input>-->
<!--					</el-form-item>-->
<!--				</el-col>-->
<!--				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">-->
<!--          <el-form-item label="卡号" prop="cardNO">-->
<!--            <el-input v-model="state.dataForm.cardNO" placeholder="请输入卡号" clearable></el-input>-->
<!--          </el-form-item>-->
<!--				</el-col>-->
<!--				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">-->
<!--          <el-form-item label="工作单位" prop="workUnit">-->
<!--            <el-input v-model="state.dataForm.workUnit" placeholder="请输入工作单位" clearable></el-input>-->
<!--          </el-form-item>-->
<!--				</el-col>-->
<!--				<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12">-->
<!--          <el-form-item label="工作地点" prop="workPlace">-->
<!--            <el-input v-model="state.dataForm.workPlace" placeholder="请输入工作地点" clearable></el-input>-->
<!--          </el-form-item>-->
<!--				</el-col>-->
<!--			</el-row>-->
<!--		</el-form>-->
<!--		<template #footer>-->
<!--			<span class="dialog-footer">-->
<!--				<el-button @click="closeDialog" size="default">取 消</el-button>-->
<!--				<el-button type="primary" @click="onSubmit" size="default">确 定</el-button>-->
<!--			</span>-->
<!--		</template>-->
<!--	</el-dialog>-->
</template>

<script setup lang="ts">
import { reactive, ref, nextTick } from 'vue';
import { ElMessage } from 'element-plus';
import { useDictData } from '/@/stores/dictData';
import dictApi from '/@/api/system/dict';
import workerApi from '/@/api/manager/worker';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const state = reactive({
	dataForm: {
		name: '', // 字典名称
		cardNo: '', // 字典类型
		workUnit: '', // 字典代码
    workPlace: '' // 角色状态
	},
	rules: {
    name: [{ required: true, message: '名字不能为空', trigger: 'blur' }],
		cardNo: [{ required: true, message: '卡号不能为空', trigger: 'blur' }],
	},
	dialog: {
		isShowDialog: false,
		isEdit: false,
		title: '',
	},
	dictTypeOption:[] as any,
});

// 打开弹窗
const openDialog = (row: any) => {
	state.dictTypeOption = useDictData().getDictItem('dict_type');
	if (row) {
		state.dialog.isEdit = true;
		state.dialog.title = '编辑员工';
		nextTick(() => {
			Object.assign(state.dataForm, row);
		});
	} else {
		state.dialog.isEdit = false;
		state.dialog.title = '添加员工';
	}
	state.dialog.isShowDialog = true;
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
	// 重置表单
	dataFormRef.value.resetFields();
};

// 提交
const onSubmit = () => {
	dataFormRef.value.validate(async (valid: boolean) => {
		if (!valid) {
			return;
		}

		if (state.dialog.isEdit) {
			//更新
      workerApi.update(state.dataForm).then((res) => {
				if (res.success) {
					ElMessage.success('更新成功');
					emit('refresh'); //刷新页面
					closeDialog();
				}
			});
		} else {
			//添加
      workerApi.add(state.dataForm).then((res) => {
				if (res.success) {
					ElMessage.success('添加成功');
					emit('refresh'); //刷新页面
					closeDialog();
				}
			});
		}
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped>
.dialog-custom {
  padding: 24px;
  background-color: var(--el-color-white);
  border-radius: 4px;
}

.dialog-content {
  display: flex;
  flex-direction: column;
}

.input-full-width {
  width: 100%;
}

.dialog-action {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
  gap: 12px;
}
</style>

